/*
 * Copyright (c) 2010-2021 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.contact

import androidx.lifecycle.MutableLiveData
import com.newlink.building.NL_App.Companion.coreContext
import com.newlink.building.activities.main.viewmodels.Module_Phone_ErrorReportingViewModel
import com.newlink.building.utils.Module_Phone_AppUtils
import java.lang.Exception
import org.linphone.core.Address
import org.linphone.core.ChatRoomSecurityLevel
import org.linphone.core.Friend

interface ContactDataInterface {
    val contact: MutableLiveData<Friend>

    val displayName: MutableLiveData<String>

    val securityLevel: MutableLiveData<ChatRoomSecurityLevel>

    val showGroupChatAvatar: Boolean
        get() = false
}

open class GenericContactData(private val sipAddress: Address) : ContactDataInterface {
    final override val contact: MutableLiveData<Friend> = MutableLiveData<Friend>()
    final override val displayName: MutableLiveData<String> = MutableLiveData<String>()
    final override val securityLevel: MutableLiveData<ChatRoomSecurityLevel> =
        MutableLiveData<ChatRoomSecurityLevel>()

    val initials = MutableLiveData<String>()

    init {
        securityLevel.value = ChatRoomSecurityLevel.ClearText
        contactLookup()
    }

    open fun destroy() {
    }

    private fun contactLookup() {
        displayName.value = sipAddress.username
        if (displayName.value.isNullOrEmpty()) {
            displayName.value = sipAddress.domain
        }
        val remoteIP = sipAddress.getHeader(coreContext.KEY_HEADER_REMOTE_IP)
        if (!remoteIP.isNullOrEmpty()) {
            displayName.value = remoteIP.toString()
        }

//        GlobalScope.launch {
//            var findContactBySipAccount = displayName.value?.let {
//                LinphoneApp.contactRepository.findContactBySipAccount(
//                    it
//                )
//            }
//
//            withContext(Dispatchers.Main) {
//                if (findContactBySipAccount?.dislayName?.isNotEmpty() == true) {
//                    displayName.value = findContactBySipAccount?.dislayName
//                }
//            }
//        }

        val c = coreContext.contactsManager.findContactByAddress(sipAddress)
        contact.value = c

        initials.value = if (c != null) {
            Module_Phone_AppUtils.getInitials(c.name ?: "")
        } else {
            Module_Phone_AppUtils.getInitials(displayName.value ?: "")
        }
    }
}

abstract class GenericContactViewModel(private val sipAddress: Address) :
    Module_Phone_ErrorReportingViewModel(),
    ContactDataInterface {
    final override val contact: MutableLiveData<Friend> = MutableLiveData<Friend>()
    final override val displayName: MutableLiveData<String> = MutableLiveData<String>()
    final override val securityLevel: MutableLiveData<ChatRoomSecurityLevel> =
        MutableLiveData<ChatRoomSecurityLevel>()

    init {
        securityLevel.value = ChatRoomSecurityLevel.ClearText
        contactLookup()
    }

    private fun contactLookup() {
        displayName.value = sipAddress.username
        if (displayName.value.isNullOrEmpty()) {
            displayName.value = sipAddress.domain
        }

        val call = coreContext.core.getCallByRemoteAddress2(sipAddress)
        val callParams = call?.remoteParams
        val remoteIP = callParams?.getCustomHeader(coreContext.KEY_HEADER_REMOTE_IP)

        remoteIP?.let {
            displayName.value = it
            try {
                sipAddress.setHeader(coreContext.KEY_HEADER_REMOTE_IP, it)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

//        GlobalScope.launch {
//            var findContactBySipAccount = displayName.value?.let {
//                FvlApplication.contactRepository.findContactBySipAccount(
//                    it
//                )
//            }
//
//            withContext(Dispatchers.Main) {
//                if (findContactBySipAccount?.dislayName?.isNotEmpty() == true) {
//                    displayName.value = findContactBySipAccount?.dislayName
//                }
//            }
//        }
        contact.value = coreContext.contactsManager.findContactByAddress(sipAddress)
    }
}
